let util = {};

util.title = function (title) {
    title = title || '智能考勤管理';
    window.document.title = title;
};

util.inOf = function (arr, targetArr) {
    let res = true;
    arr.map(item => {
        if (targetArr.indexOf(item) < 0) {
            res = false;
        }
    });
    return res;
};

util.oneOf = function (ele, targetArr) {
    if (targetArr.indexOf(ele) >= 0) {
        return true;
    } else {
        return false;
    }
};

util.showThisRoute = function (itAccess, currentAccess) {
    if (typeof itAccess === 'object' && Array.isArray(itAccess)) {
        return util.oneOf(currentAccess, itAccess);
    } else {
        return itAccess === currentAccess;
    }
};

util.getPathObjByName = function (vm, name) {
    let pathObj = vm.$store.state.routers.filter((item) => {
        if (item.children.length <= 1) {
            return item.name === name;
        } else {
            let i = 0;
            let childArr = item.children;
            let len = childArr.length;
            while (i < len) {
                if (childArr[i].name === name) {
                    return true;
                }
                i++;
            }
            return false;
        }
    })[0];
    return pathObj;
};

util.setCurrentPath = function (vm, name) {
    let title = '';
    vm.$store.state.routers.forEach(item => {
        if (item.children.length === 1) {
            if (item.children[0].name === name) {
                title = item.title;
            }
        } else {
            item.children.forEach(child => {
                if (child.name === name) {
                    title = child.title;
                }
            });
        }
    });
    let currentPathArr = [];
    if (name === 'home_index') {
        currentPathArr = [
            {
                title: '首页',
                path: '',
                name: 'home_index'
            }
        ];
    } else if (name.indexOf('_index') >= 0 && name !== 'home_index') {
        currentPathArr = [
            {
                title: '首页',
                path: '/home',
                name: 'home_index'
            },
            {
                title: title,
                path: '',
                name: name
            }
        ];
    } else {
        let currentPathObj = vm.$store.state.routers.filter(item => {
            if (item.children.length <= 1) {
                return item.children[0].name === name;
            } else {
                let i = 0;
                let childArr = item.children;
                let len = childArr.length;
                while (i < len) {
                    if (childArr[i].name === name) {
                        return true;
                    }
                    i++;
                }
                return false;
            }
        })[0];
        if (currentPathObj.children.length <= 1 && currentPathObj.name === 'home') {
            currentPathArr = [
                {
                    title: '首页',
                    path: '',
                    name: 'home_index'
                }
            ];
            // } else if (currentPathObj.children.length <= 1 && currentPathObj.name !== 'home') {
            //     currentPathArr = [
            //         {
            //             title: '首页',
            //             path: '/home',
            //             name: 'home_index'
            //         },
            //         {
            //             title: currentPathObj.title,
            //             path: '',
            //             name: name
            //         }
            //     ];
        } else {
            let childObj = currentPathObj.children.filter((child) => {
                return child.name === name;
            })[0];
            currentPathArr = [
                {
                    title: '首页',
                    path: '/home',
                    name: 'home_index'
                },
                {
                    title: currentPathObj.title,
                    path: '',
                    name: currentPathObj.name
                },
                {
                    title: childObj.title,
                    path: currentPathObj.path + '/' + childObj.path,
                    name: name
                }
            ];
        }
    }
    vm.$store.commit('setCurrentPath', currentPathArr);

    return currentPathArr;
};

util.openPage = function (vm, name, title) {
    vm.$router.push({
        name: name
    });
    let hasOpened = false;
    vm.pageTagsList.forEach((item, index) => {
        if (item.name === name) {
            hasOpened = true;
            vm.$store.commit('moveToSecond', index);
        }
    });
    if (!hasOpened) {
        vm.$store.commit('increateTag', {name: name, title: title});
    }
};

util.openNewPage = function (vm, name, argu) {
    let tagHasOpened = false;
    if (!tagHasOpened) {
        let tag = vm.$store.state.tagsList.filter((item) => {
            if (item.children) {
                return name === item.children[0].name;
            } else {
                return name === item.name;
            }
        });
        tag = tag[0];
        tag = tag.children ? tag.children[0] : tag;
        if (argu) {
            tag.argu = argu;
        }
    }
    vm.$store.commit('setCurrentPageName', name);
};

util.deepCopy = function (obj) {
    var str, newobj = obj.constructor === Array ? [] : {};
    if(typeof obj !== 'object'){
        return;
    } else if(window.JSON){
        str = JSON.stringify(obj), //系列化对象
            newobj = JSON.parse(str); //还原
    } else {
        for(var i in obj){
            newobj[i] = typeof obj[i] === 'object' ?
                cloneObj(obj[i]) : obj[i];
        }
    }
    return newobj;
}

util.downloadByIframe = function (url) {
    var iframe = document.getElementById("myIframe");
    if (!iframe) {
        iframe = document.createElement("iframe");
        iframe.style.display = "none";
        iframe.id = "myIframe";
        document.body.appendChild(iframe);
    }
    iframe.src = url;
}

// 保存base64图片
util.saveBase64Img = function(url,name){
    const aEl = document.createElement('a');
    const bodyEl = document.body;
    aEl.href = url;
    aEl.download = name ? name : +new Date() + '.jpg'
    bodyEl.appendChild(aEl);
    aEl.click();
    bodyEl.removeChild(aEl);
}

// 保存图片
util.saveImg = function(url,name){
    const aEl = document.createElement('a');
    const bodyEl = document.body;
    aEl.href = url;
    aEl.target = '_blank';
    aEl.download = name ? name : +new Date() + '.jpg'
    bodyEl.appendChild(aEl);
    aEl.click();
    bodyEl.removeChild(aEl);
}

function getBlob(url){
    return new Promise((resolve,reject)=>{
        const xhr = new XMLHttpRequest()
        xhr.open('GET',url, true)
        xhr.setRequestHeader('Content-type','image/jpeg')
        xhr.responseType = 'blob';
        xhr.onload = () => {
            if (xhr.status === 200) {
                resolve(xhr.response);
            }
        };
        xhr.send();
    })
}
function saveAs(blob, filename) {
    if (window.navigator.msSaveOrOpenBlob) {
        navigator.msSaveBlob(blob, filename);
    } else {
        const link = document.createElement('a');
        const body = document.querySelector('body');
        link.href = window.URL.createObjectURL(blob);
        link.download = filename;
        link.style.display = 'none';
        body.appendChild(link);
        link.click();
        body.removeChild(link);
        window.URL.revokeObjectURL(link.href);
    }
}

// 下载图片
util.downloadImg = function(url,name){
    name ? name : +new Date() + '.jpg'
    getBlob(url).then(blob=>{
        saveAs(blob,name)
    })
}

export default util;
